require('../../assets/css/index.js')
require('../../assets/fonts/font/iconfont.css')
require('../../assets/lib/swiper/swiper-bundle.css')
let Swiper = require('../../assets/lib/swiper/swiper-bundle.js')
require('../../utils/local.js')
let https = require('../../utils/https.js')
require('../../assets/lib/weui/weui.css')
let weui = require('../../assets/lib/weui/weui.js')


// 引入公共js
let dom = require('../../utils/dom.js')
const local = require('../../utils/local.js')
const { default: axios } = require('axios')
require('./circleAdd.less')
// 当前页面js
dom.isLogin()
dom.ready(function () {

  dom.getEle('.close').addEventListener("click", function () {
    window.history.go(-1)
  })
  // 上传图片
  // 输入框改变事件绑定
  let arr = [];
  // dom.getEle("#pic").addEventListener("change", function () {
  //   let pic = this.files[0];
  //   let fd = new FormData();
  //   fd.append("file", pic)
  //   axios.post(https.hostUrl + '/api/shared/uploadPortrait1', fd)
  //     .then(function (res) {
  //       console.log(res);
  //       if (res.data.errno === 0) {
  //         dom.getEle('.list').innerHTML += `
  //         <li class="add">
  //         <img src="${https.hostUrl + res.data.data.url}" alt="">
  //         </li>
  //           `
  //         arr.push(res.data.data.url);
  //         console.log(arr);
  //       }
  //     })
  // })
  dom.getEle('#pic').addEventListener("change", function () {
    let pic = this.files[0];
    
    // console.log(pic);
    // 拼接后端上传所需文件
    let fd = new FormData();
    fd.append('file', pic);
    console.log(fd.get('file'));
    https.post('/api/shared/uploadPortrait1', fd, function (res) {
      if (res.errno == 0) {
        // console.log(res.data);
        

        dom.getEle('.list').innerHTML += `
    <li class="add">
    <img src="${https.hostUrl + res.data.url}" alt="">
  </li>
      `
        arr.push(res.data.url);
        // console.log(arr);
        dom.getEle('#pic').value = ''
      }
    }
    )

  })
  // 输入框字数
  dom.getEle('#think').addEventListener("input", function () {
    if (dom.getEle('#think').value.length > 140) {
      dom.getEle('#think').value = (this.value).slice(0, 140)
    } else {
      dom.getEle('#limit').innerHTML = dom.getEle('#think').value.length;

    }
  })
  // 省份选择
  let userData = local.get('userInfo');
  // console.log(userData);
  let releaseProvinceId;
  let provinceId;
  let releaseCityId;
  dom.getEle('.province').addEventListener("click", function () {

    https.get('/api/shared/province', {}, function (res) {
      // console.log(res);
      if (res.errno == 0) {
        let provdata = res.data.map(v => {

          return {
            label: v.name,
            value: v.id
          }
        })
        weui.picker(provdata, {
          onConfirm: function (result) {
            // console.log('确认事件', result);
            dom.getEle('#province').innerHTML = result[0].label;
            // console.log(result[0].value);
            provinceId = result[0].value;
            // console.log(provinceId)
            releaseProvinceId = result[0].value;
            dom.getEle('.city').addEventListener("click", function () {
              if (provinceId) {
                https.get('/api/shared/city', { provinceId }, function (res) {
                  if (res.errno == 0) {
                    let provdata = res.data.map(v => {
                      // console.log(v);
                      return {
                        label: v.name,
                        value: v.id
                      }
                    })
                    weui.picker(provdata, {
                      onConfirm: function (result) {
                        dom.getEle('#city').innerHTML = result[0].label;
                        releaseCityId = result[0].value
                      },
                      title: '请选择城市'
                    })
                  }
                });
              }
            })
          },
          title: '请选择省份'
        })
      }
    });
  })

  //城市选择
  dom.getEle('.deliver').addEventListener("click", function () {
    getDeliver()
    alert("发表成功")
  })

  function getDeliver() {
    https.post('/api/circles/opinion',
      {
        content: dom.getEle('#think').value,
        releaseProvinceId,
        releaseCityId,
        imgUrls:arr

      }, (res) => {
        if (res.errno == 0) {

          console.log(res);

          // res.data.map((v,i)=>{
          //   console.log(v);  //拿到了





          // })

        }
      })
  }
 

 

})